<!DOCTYPE html>
<html>
<?php $this->load->view('admin/public/header.html');?>
<body class="childrenBody" style="width:50%;">
<form class="layui-form layui-row layui-col-space10">
	<div class="layui-col-md9 layui-col-xs12">
		<div class="layui-row layui-col-space10">
			<div class="layui-col-md9 layui-col-xs7">
				
				<div class="layui-form-item magt3 typeIcon" >
							<div class="layui-row layui-col-space10">
								<div class="layui-col-md12 layui-col-xs6">
									<label class="layui-form-label">图片</label>
									<div class="layui-upload-list thumbBox posterBox mag0 magt3">
										<img class="layui-upload-img thumbImg posterimg"  src="">
										<input type="hidden" class="poster" lay-verify="" value="" name="url">
									</div>
								</div>
							</div>
				</div>
					
				<div class="layui-form-item magt3">
					<label class="layui-form-label">礼物名称</label>
					<input type="hidden" class="giftId" name="giftId" value="">
					<div class="layui-input-block">
						<input type="text" class="layui-input title" name="name" required placeholder="请输入分类名称">
					</div>
				</div>
				<div class="layui-form-item magt3">
					<label class="layui-form-label">价格</label>
					<div class="layui-input-block">
						<input type="number" class="layui-input title" name="price" lay-verify="displayOrder" placeholder="请输入礼物价格">
					</div>
				</div>
				
			<div class="layui-right">
				<a class="layui-btn layui-btn-sm" lay-filter="addType" lay-submit><i class="layui-icon">&#xe609;</i>保存</a>
			</div>
		
			</div>
			
		</div>
		
	</div>	
</form>
<script type="text/javascript">
layui.use(['form','layer','layedit','laydate','upload'],function(){
	var form=layui.form, $=layui.jquery,upload=layui.upload;
	var layerindex;		//弹层句柄
	
	
    //上传缩略图
    upload.render({
        elem: '.posterBox',
        //multiple:true,
        url: '<?php echo site_url("upload/upload")?>',
        before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
        	layerindex=layer.load(); //上传loading
        },
        done: function(res, index, upload){
        	layer.close(layerindex);
            if(res.code==200){
            	layer.msg('上传成功');
            	var url=res.content.url
            	var src='<?php echo IMAGEHOST?>'+url;
            	$('.posterimg').attr('src',src);
                $('.thumbBox').css("background","#fff"); 
                $(".poster").val(url);
            }else{
            	layer.msg('上传失败');
            }
        }
    });
	form.verify({
	    typeName : function(val){
	        if(val == ''){
	            return "分类名称不能为空";
	        }
	    },
	    displayOrder : function(val){
	        if(val == ''){
	            return "排序值不能为空";
	        }
	    },

	})
	form.on("submit(addType)",function(data){
    	
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        
        $.ajax({
        	type:'post',
        	data:data.field,
        	url:'addGift',
        	dataType: "json", 
        	success:function(res){
        		if(res==200){
        			top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
        		}else{
        			top.layer.close(index);
                    top.layer.msg("添加失败！");
        		}
        	},
        	fail:function(){
        		top.layer.close(index);
                top.layer.msg("添加失败！");
        	}
        })        
        
    })
})

</script>
</body>
</html>